package wsz.pojo;

import lombok.Data;
import wsz.mapper.MapperRegistry;
import wsz.sqlSession.SqlSession;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 封装mysql的连接信息与mapper
 * @author wsz
 * @date 2021/10/1 0:47
 **/
@Data
public class Configuration {

    private DataSource dataSource;

    // mapper注册
    private MapperRegistry mapperRegistry;

    public Configuration() {
        this.mapperRegistry = new MapperRegistry();
    }

    /**
     * key:statementId=namespace.id
     * value: MappedStatement
     */
    Map<String, MappedStatement> statementMap = new HashMap<>();

    public <T> void addMapper(Class<T> type) {
        this.mapperRegistry.addMapper(type);
    }

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        return this.mapperRegistry.getMapper(type, sqlSession);
    }

    /**
     * 判断是否存在对应的statement
     * @param statementName
     * @return
     */
    public boolean hasStatement(String statementName) {
        return this.statementMap.containsKey(statementName);
    }

    /**
     * 获取对应的statement
     * @param id
     * @return
     */
    public MappedStatement getMappedStatement(String id) {
        return this.statementMap.get(id);
    }
}
